package jwdt.xadmin.info.action;

import jwdt.common.action.BaseAction;
import jwdt.xadmin.info.bean.TNews;
import jwdt.xadmin.info.bean.TType;
import jwdt.xadmin.info.service.NewsManageService;
import jwdt.xadmin.info.service.TypeManageService;
import jwdt.xadmin.system.bean.TUser;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.DateUtil;
import com.ezerg.web.Form;
import com.ezerg.web.Module;
import com.ezerg.web.Page;
import com.ezerg.web.annotation.Controller;
import com.ezerg.web.annotation.RequestMapping;
import com.ezerg.web.fileupload.FileItem;

@Controller("/xadmin/info")
public class NewsManageAction extends BaseAction {
	
	TypeManageService typeManageService = app.getBean("TypeManageService");
	NewsManageService newsManageService = app.getBean("NewsManageService");
	
	/**
	 * 信息添加
	 * @param f
	 * @param m
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/newsAdd")
	public Page doNewsAdd(Form f, Module m) throws Exception {
		int typeid = f.getInt("typeid", 1);
		f.add("xxejlm", Jdb.get().query("SELECT mxdm,mxmc FROM jwdt_zd_flmx WHERE flid=1 ORDER BY px"));
		f.add("type", typeManageService.selectOneType(typeid));
		return m.findPage("/xadmin/info/newsAdd");
	}
	
	/**
	 * 信息保存
	 * @param f
	 * @param m
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/newsSave")
	public Page doNewsSave(Form f, Module m) throws Exception {
		TUser userInfo = (TUser)f.getSession("userInfo");
		String user = userInfo.getUsername();
		TType type = (TType)Jdb.get().get(TType.class, f.getInt("typeid"));
		if(type == null) {
			f.add("ret", "您所选择的栏目不存在");
			return m.findPage("tips");
		}
		TNews news = (TNews)f.copy2Class(TNews.class);
		news.setEditor(user);
		news.setUpdatetime(DateUtil.getDateTime());
		String picurl = f.getString("picurl");
		if(!"".equals(picurl)) {
			news.setPicnews(1);
			news.setPicname(picurl);
		} else {
			news.setPicnews(0);
		}
		// 兼容 ewebeditor 和 fckeditor
		String savedfilename = f.getString("d_savefilename");
		//System.out.println("savedfilename="+savedfilename);
		if(savedfilename.equals("")) {// fckeditor 
			news.setSavefilename(picurl);
		} else {
			news.setSavefilename(savedfilename);
		}
		// 栏目默认审核
		news.setChecked(type.getZt());
		String ret = "添加成功";
		try {
			Jdb.get().add(news);
			Jdb.get().execute("update t_user set number=number+1 where username='"+user+"'");
		} catch (Exception e) {
			e.printStackTrace();
			ret = "添加出错";
		}
		f.add("ret", ret);
		return doNewsAdd(f, m);
	}
	
	/**
	 * 我的信息列表
	 * @param f
	 * @param m
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/selectNews")
	public Page doSelectNews(Form f, Module m) throws Exception {
		TUser userInfo = (TUser)f.getSession("userInfo");
		PageUtil pu = (PageUtil) f.copy2Class(PageUtil.class);
		String keyword = f.getString("keyword");
		String typeid = f.getString("typeid");
		String updatetime = f.getString("updatetime");
		return m.getWritePage(f, newsManageService.selectNewsList(pu, typeid, keyword, userInfo.getUsername(),updatetime,null).toJson());
	}
	
	/**
	 * 信息编辑
	 * @param f
	 * @param m
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/newsEdit")
	public Page doNewsEdit(Form f, Module m) throws Exception {
		String type = f.getString("type", "my");
		String newsid = f.getString("newsid");
		TNews obj = (TNews)Jdb.get().get(TNews.class, newsid);
		if(obj == null) {
			f.add("ret", "您要修改的记录不存在");
			return m.findPage("tips");
		}
		f.add("obj", obj).add("type", type);
		f.add("xxejlm", Jdb.get().query("SELECT mxdm,mxmc FROM jwdt_zd_flmx WHERE flid=1 ORDER BY px"));
		return m.findPage("/xadmin/info/newsEdit");
	}
	
	@RequestMapping("/newsEditSave")
	public Page doEditact(Form f, Module m) throws Exception {
		// 
		String type = f.getString("type", "my");
		String newsid = f.getString("newsid");
		TNews obj = (TNews)Jdb.get().get(TNews.class, newsid);
		if(obj == null) {
			f.add("ret", "您要修改的记录不存在");
			return m.findPage("info");
		}
		obj = (TNews)f.copy2Obj(obj);
		obj.setUpdatetime(obj.getUpdatetime()+" "+DateUtil.getDateTime().substring(11));
		String picurl = f.getString("picurl");
		if(!"".equals(picurl)) {
			obj.setPicnews(1);
			obj.setPicname(picurl);
		} else {
			obj.setPicnews(0);
		}
		// 兼容 ewebeditor 和 fckeditor
		String savedfilename = f.getString("d_savefilename");
		//System.out.println("savedfilename="+savedfilename);
		if(savedfilename.equals("")) {// fckeditor 
			obj.setSavefilename(picurl);
		} else {
			obj.setSavefilename(savedfilename);
		}
		String ret = "修改成功";
		try {
			Jdb.get().update(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "修改出错";
		}
		f.add("ret", ret).add("type", type);
		return doNewsEdit(f, m);
	}
	
	/**
	 * 信息删除
	 * @param f
	 * @param m
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delmyNews")
	public Page doDelMyNews(Form f, Module m) throws Exception {
		int newsid = f.getInt("newsid");
		return m.getJsonPage(f,newsManageService.delMyNews(newsid));
	}
	
	@RequestMapping("/updateType")
	public Page doUpdateType(Form f, Module m) throws Exception {
		int newsid = f.getInt("newsid");
		int typeid = f.getInt("typeid");
		TNews obj = (TNews)Jdb.get().get(TNews.class, newsid);
		if(obj == null) {
			f.add("ret", "您要修改的记录不存在");
			return m.findPage("info");
		}
		try {
			Jdb.get().execute("update t_news set typeid=? where id=?", new Object[]{typeid, newsid}, false);
			obj.setTypeid(typeid);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return m.getWritePage("y");
	}
	
	/**
	 * 信息审核
	 * @param f
	 * @param m
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/myNewsCheck")
	public Page doMyNewsCheck(Form f, Module m) throws Exception {
		PageUtil pu = (PageUtil) f.copy2Class(PageUtil.class);
		TNews news = (TNews)f.copy2Class(TNews.class);
		//
		String title = null, username = null;
		int searchType = f.getInt("searchtype");
		String keyword = f.getString("keyword");
		String typeid = f.getString("typeid");
		String updatetime = f.getString("updatetime");
		//
		if(searchType == 0) {
			title = keyword;
		} else if(searchType == 1) {
			username = keyword;
		}
		return m.getWritePage(f, newsManageService.selectNewsList(pu, typeid, title, username,updatetime,news).toJson());
	}
	
	/**
	 * 审核操作
	 * @param f
	 * @param m
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/myNewsChkact")
	public Page doMyNewsChkact(Form f, Module m) throws Exception {
		int newsid = f.getInt("id");
		int checked = f.getInt("state");
		return m.getWritePage(f, newsManageService.newsChkact(newsid, checked));
	}
	
	/**
	 * 推荐操作
	 * @param f
	 * @param m
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/myNewsGood")
	public Page doMyNewsGood(Form f, Module m) throws Exception {
		int newsid = f.getInt("id");
		int goodnews = f.getInt("state");
		return m.getWritePage(f, newsManageService.newsGood(newsid, goodnews));
	}
	
	/**
	 * 置顶操作
	 * @param f
	 * @param m
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/myNewsTOP")
	public Page doMyNewsTOP(Form f, Module m) throws Exception {
		int newsid = f.getInt("id");
		int istop = f.getInt("state");
		return m.getWritePage(f, newsManageService.newsTop(newsid, istop));
	}
	/**
	 * 精华信息操作
	 * @param f
	 * @param m
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/myNewsDaodu")
	public Page doMyNewsDaodu(Form f, Module m) throws Exception {
		int newsid = f.getInt("id");
		int daodu = f.getInt("state");
		return m.getWritePage(f, newsManageService.newsDaodu(newsid, daodu));
	}
	
	@RequestMapping("/addNewsImg")
	public Page doAddNewsImg(Form f, Module m) throws Exception {
		FileItem file = f.getFileItem("imgfile");
		return m.getWritePage(f, newsManageService.doAddact(file));
	}
	
	@RequestMapping("/newsView")
	public Page doNewsView(Form f, Module m) throws Exception {
		String type = f.getString("type", "my");
		String newsid = f.getString("newsid");
		TNews obj = (TNews)Jdb.get().get(TNews.class, newsid);
		if(obj == null) {
			f.add("ret", "您要修改的记录不存在");
			return m.findPage("tips");
		}
		f.add("obj", obj).add("type", type);
		return m.findPage("/xadmin/info/newsView");
	}
	
}
